//
//  ZQNavigationController.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/3.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQNavigationController.h"

@interface ZQNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation ZQNavigationController
+ (void)load
{
    //获取当前类的所有导航条
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //设置 文本加粗 bold
    dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    
    bar.titleTextAttributes = dict;
    
    //设置导航条背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 全屏滑动: 分析:为什么导航控制器的滑动手势只能边缘滑动
    
    /*
     UIScreenEdgePanGestureRecognizer
     滑动手势
     // 触发UIScreenEdgePanGestureRecognizer就会有滑动功能
     // 触发UIScreenEdgePanGestureRecognizer就会调用它的target的action
     // UIScreenEdgePanGestureRecognizer的targer的action => 滑动返回功能
     
     <UIScreenEdgePanGestureRecognizer: 0x7fc310eb72c0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fc310eb5810>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fc310eb75e0>)>>
     
     */
    
    // target=<_UINavigationInteractiveTransition 0x7fc310eb75e0>)
    
    //  <_UINavigationInteractiveTransition: 0x7f9c7234d010>:滑动手势代理
    
    
    //创建一个全局手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    //添加到控件中
    [self.view addGestureRecognizer:pan];
    //设置手势的代理,  使控制器可以在什么时候出发
    pan.delegate = self;
    
    //将系统的手势清空
    self.interactivePopGestureRecognizer.enabled = NO;
//    self.interactivePopGestureRecognizer.delegate = self;
    
}


//是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //在根控制器下不要触发手势
    return self.childViewControllers.count > 1 ;
}


//push之前实现该方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    //判断是否为非根控制器
    if (self.childViewControllers.count > 0) {
        
        
        //判断非根控制器,隐藏底部控制条
        viewController.hidesBottomBarWhenPushed = YES;
        
        
            //创建一个按钮.设置 文字. 图片, 方法,
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [backButton sizeToFit];
        
        //设置按钮的偏移量   注意: 要在设置尺寸后设置 才可生效
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
        
        //添加到控制器左上角按钮中
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    }
    
    //调用父类方法 执行跳转push
    [super pushViewController:viewController animated:animated];

}

- (void)back
{
    NSLog(@"点击了返回");
    [self popToRootViewControllerAnimated:YES];
}
@end
